package com.hf.eagle.web.struts.admin.manager;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.GenericValidator;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hf.eagle.domain.DeptInfo;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.Pager;

/**
 * @author Xu,XiaoYuan
 * @version 2011.10.31 22:21
 */
public class DeptInfoAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.list(mapping, form, request, response);
	}

	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;
		Pager pager = (Pager) dynaBean.get("pager");
		String dept_name_like = (String) dynaBean.get("dept_name_like");

		DeptInfo deptInfo = new DeptInfo();
		deptInfo.setIs_del(0);
		super.copyProperties(deptInfo, form);
		deptInfo.getMap().put("dept_name_like", dept_name_like);

		Long recordCount = super.getFacade().getDeptInfoService().getDeptInfoCount(deptInfo);
		pager.init(recordCount, pager.getPageSize(), pager.getRequestPage());
		deptInfo.getRow().setFirst(pager.getFirstRow());
		deptInfo.getRow().setCount(pager.getLastRow());

		List<DeptInfo> list = super.getFacade().getDeptInfoService().getDeptInfoPaginatedList(deptInfo);
		request.setAttribute("entityList", list);

		super.copyProperties(form, deptInfo);

		return mapping.findForward("list");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		return mapping.findForward("input");
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;
		String dept_id = (String) dynaBean.get("dept_id");

		DeptInfo deptInfo = new DeptInfo();
		deptInfo.setDept_id(Long.valueOf(dept_id));
		deptInfo.setIs_del(0);
		deptInfo = super.getFacade().getDeptInfoService().getDeptInfo(deptInfo);
		if (null != deptInfo) {
			deptInfo.setQueryString(super.serialize(request, "dept_id", "method", "mod_id"));
		}
		super.copyProperties(form, deptInfo);

		return mapping.findForward("input");
	}

	public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;
		String dept_id = (String) dynaBean.get("dept_id");

		DeptInfo newsInfo = new DeptInfo();
		newsInfo.setDept_id(Long.valueOf(dept_id));
		newsInfo = super.getFacade().getDeptInfoService().getDeptInfo(newsInfo);

		super.copyProperties(form, newsInfo);

		return mapping.findForward("view");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String mod_id = (String) dynaBean.get("mod_id");

		DeptInfo deptInfo = new DeptInfo();
		super.copyProperties(deptInfo, form);
		deptInfo.setIs_del(0);

		if (null == deptInfo.getDept_id()) {
			deptInfo.setPar_id(0l);
			super.getFacade().getDeptInfoService().createDeptInfo(deptInfo);
			saveMessage(request, "entity.inserted");
		} else {
			super.getFacade().getDeptInfoService().modifyDeptInfo(deptInfo);
			saveMessage(request, "entity.updated");
		}

		// the line below is added for pagination
		StringBuffer pathBuffer = new StringBuffer();
		pathBuffer.append(mapping.findForward("success").getPath());
		pathBuffer.append("&mod_id=" + mod_id);
		pathBuffer.append("&");
		pathBuffer.append(super.encodeSerializedQueryString(deptInfo.getQueryString()));
		ActionForward forward = new ActionForward(pathBuffer.toString(), true);
		// end
		return forward;
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String dept_id = (String) dynaBean.get("dept_id");
		String mod_id = (String) dynaBean.get("mod_id");
		String[] pks = (String[]) dynaBean.get("pks");

		DeptInfo deptInfo = new DeptInfo();
		deptInfo.setIs_del(1);
		if (!StringUtils.isBlank(dept_id) && GenericValidator.isLong(dept_id)) {
			deptInfo.setDept_id(Long.valueOf(dept_id));
			super.getFacade().getDeptInfoService().removeDeptInfo(deptInfo);
			saveMessage(request, "entity.deleted");
		} else if (!ArrayUtils.isEmpty(pks)) {
			deptInfo.getMap().put("pks", pks);
			super.getFacade().getDeptInfoService().removeDeptInfo(deptInfo);
			saveMessage(request, "entity.deleted");
		}

		// the line below is added for pagination
		StringBuffer pathBuffer = new StringBuffer();
		pathBuffer.append(mapping.findForward("success").getPath());
		pathBuffer.append("&mod_id=" + mod_id + "&");
		pathBuffer.append(encodeSerializedQueryString(serialize(request, "dept_id", "method", "mod_id")));
		ActionForward forward = new ActionForward(pathBuffer.toString(), true);
		// end
		return forward;
	}
}
